//
//  PNode.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 8/16/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "PNode.h"
#import "Path.h"
#import "PLine.h"
#import "MainGameScene.h"

@implementation PNode
@synthesize _nextNode = mNextNode, _type = mType, _state = mState, _line = mLine;

-(id) initWithType:(eNodeType)type
{
    if (self = [super initWithSpriteFrameName:[self getSpriteFrameName]])
    {
        mType = type;
        [self createBody];
    }
    
    return self;
}

-(void) createBody
{
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(0, 0);
    
    _b2Body = PhysicSystem::getInstance()->mWorld->CreateBody(&bodyDef);
    
    b2CircleShape shape;
    shape.m_radius = (self.contentSize.width / 2) / PTM_RATIO;
    
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &shape;
    fixtureDef.filter.categoryBits = [self getObjectType];
    fixtureDef.filter.maskBits = (mType == eNT_USER_NODE) ? MASK_BIT_USER_NODE : MASK_BIT_PATH_NODE;
    fixtureDef.isSensor = true;
    _b2Body->CreateFixture(&fixtureDef);
    
    [self setPTMRatio:PTM_RATIO];
    _b2Body->SetUserData(self);
}

-(OBJECT_TYPE) getObjectType
{
    if (mType == eNT_USER_NODE) return eO_USER_NODE;
    else return eO_PATH_NODE;
}

-(NSString *) getSpriteFrameName
{
    return SFN_PNODE;
}

-(Path *) getPath
{
    return (Path *)self.parent.parent;
}

-(BOOL) isTail
{
    return (mNextNode == nil);
}

-(void) spread
{
    if ([MainGameScene getInstance]._mainGameState == sMG_ENDING)
        return;
    
    ccColor3B cl_v = CL_VIRUS;
    CCTintTo *tintTo = [CCTintTo actionWithDuration:PATH_NODE_SPREAD_TIME red:cl_v.r green:cl_v.g blue:cl_v.b];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self infected];
    }];     
    CCSequence *sequence = [CCSequence actions:tintTo, callBlock, nil];
    [self runAction:sequence];
    
    
//    [self spreadDone];
}

-(void) infected
{
    [self setColor:CL_VIRUS];
    mVirus = [CCSprite spriteWithSpriteFrameName:SFN_VIRUS];
    [mVirus setPosition:[self getRealPosition]];
    [mVirus setScale:0];
    [[MainGameScene getInstance]._globalBatch addChild:mVirus z:Z_ORDER_VIRUS];
    
    CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.1 scale:1];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [self spreadDone];
    }];
    CCSequence *sequence = [CCSequence actions:scaleTo, callBlock, nil];
    [mVirus runAction:sequence];
    
    [self schedule:@selector(updateVirus)];
}

-(void) updateVirus
{
    [mVirus setPosition:[self getRealPosition]];
}

-(void) spreadDone
{
    CCLOG(@"node spread done");
    if (mType == eNT_USER_NODE)
    {
        [[MainGameScene getInstance] endGame:eGT_SPREAD_LINE];
        return;
    }
    
    if (mNextNode._state == eNS_CONNECTING && mState == eNS_CONTECTED)
    {
        [[[MainGameScene getInstance] getUserLine] spread];
    }
    
    mState = eNS_SPREADED;
    [mLine spread];
}

-(void) addActions:(NSArray *)actions
{
    mListAction = [actions retain];
}

-(void) startAction
{
    
    if (mListAction != nil)
    {
        for (int i = 0; i < mListAction.count; i++)
        {
            [self runAction:[mListAction objectAtIndex:i]];
        }
    }
}

-(void) stopAction
{
    [self stopAllActions];
}

-(void) dealloc
{
    [self stopAllActions];
    [self unscheduleAllSelectors];
    [super dealloc];
    
    CCLOG(@"PNode dealloc!");
}

@end
